package com.mnotice.web.controller;


import com.mnotice.common.constants.InformerEnum;
import com.mnotice.common.entity.message.Notification;
import com.mnotice.message.informer.JsonFormat;
import com.mnotice.message.sender.MessageSender;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class SendMsgController {
    @Resource
    private FreeMarkerConfigurer configurer;
    //注入RabbitMQ的模板
    @Resource
    private MessageSender sender;

    @GetMapping("/sendmsg")
    public String sendMsg() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            Notification notification = new Notification();
            notification.setContent("{\"name\":\"testaaaaa\"}");
            notification.setContentType("text");
            notification.setInformMethod(InformerEnum.Email);
            notification.setTemplateName("test");
            notification.setProjectId(1);
            sender.send("item.test", notification);
        }
        return "发送成功用时:" + (System.currentTimeMillis() - start);
    }

    @GetMapping("/html")
    public String html() throws IOException, TemplateException {
        Template template = configurer.getConfiguration().getTemplate("1_test");
        Map<String, Object> model = new HashMap<>();
        JsonFormat format = new JsonFormat("{\"name\":\"Geticsen\"}");
        model.put("format", format);
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }
}
